{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# RWET Programming Exercise B: Lists and list operations\n",
    "\n",
    "To do this exercise, download this notebook and open it on your own computer. There are several tasks described below. Your job is to change the code in the cells so that the output from running the cell matches the expected output indicated above the cell."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Task 1: Simple indexing and list functions\n",
    "\n",
    "There is a list of numbers assigned to a variable `number_list` in the code below that has been assigned a list of numbers. Run this cell before filling in the answers below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "number_list = [-2, -58, 4, 36, -6, 60, -57]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Write another expression in the cell below, using square bracket index notation, that causes the 4th element of `number_list` (i.e., the number `36`) to be displayed when running the code in the cell.\n",
    "\n",
    "Expected output: `36`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now write an expression that evaluates to the number of items in the list (i.e., `7`), using the `len()` function.\n",
    "\n",
    "Expected output: `7`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The following expression:\n",
    "\n",
    "    print max(number_list)\n",
    "    \n",
    "... will print the largest value in `number_list` (i.e., `60`). Change the variable `x` in the code below so that the expression\n",
    "\n",
    "    print sorted(number_list)[x]\n",
    "    \n",
    "... does the same thing. (i.e., when you run the cell, it should display `60`.)\n",
    "\n",
    "Expected output: `20`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-58"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = 0\n",
    "sorted(number_list)[x]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Task 2: List slices\n",
    "\n",
    "Write an expression below that evaluates to a slice of `number_list` starting with its second element and ending with its fifth element (exclusive).\n",
    "\n",
    "Expected output: `[-2, -58, 4, 36, -6, 60, -57]`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now, write an expression below that evaluates to a slice of `number_list` starting with its third element and ending at the end of the list.\n",
    "\n",
    "Expected output: `[4, 36, -6, 60, -57]`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finally, fill in a value for the variable `x` below so that the expression below it evaluates to a slice of `number_list` starting at the second-to-last element of the list and ending at the end of the list. (Hint: `x` should be a negative integer.)\n",
    "\n",
    "Expected output: `[60, -57]`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[-2, -58, 4, 36, -6, 60, -57]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = 0\n",
    "number_list[x:]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Task 3: List comprehensions\n",
    "\n",
    "For this problem set, I'm introducing a new Python operator: the modulo operator, `%`. This operator returns the *remainder* of dividing one integer by another. For example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "22 % 3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This expression evaluates to `1` because the remainder of dividing `22` by `3` is `1`. We can use the modulo operator to test whether or not a number is even (i.e., divisible by 2), by using the number `2` on the right side of the operator:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "100 % 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "101 % 2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Given the above information, write a list comprehension that evaluates to a list containing *only* the members of `number_list` that are *divisible by three*. Use the modulo operator in the membership expression of the list comprehension.\n",
    "\n",
    "Expected output: `[36, -6, 60, -57]`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Task 4: Splitting strings\n",
    "\n",
    "In the cell below, a variable `float_str` is set to a string containing a list of floating-point numbers, separated by semicolons (`;`). (Make sure to run this cell before you proceed, so that the variable will be available in subsequent cells.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "float_str = \"5.8;6.9;3.1;5.9;6.6;6.5;6.5;5.6;6;6.4;3.32;6.0;6.0;6.3;6.6;6.6\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Write an expression below that converts this string into a list of floating-point numbers. The type of the expression should be `list` and the type of individual elements in the list should be `float`. Hint: You'll need to use the `float()` function, the `.split()` method, and a list comprehension.\n",
    "\n",
    "Expected output:\n",
    "\n",
    "    [5.8, 6.9, 3.1, 5.9, 6.6, 6.5, 6.5, 5.6, 6.0, 6.4, 3.32, 6.0, 6.0, 6.3, 6.6, 6.6]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using the expression you wrote above as a starting point, write an expression below that evaluates to the *sum* of the numbers in the list.\n",
    "\n",
    "Expected output: `94.11999999999999` (or close to that)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Task 5: Strings in list comprehensions\n",
    "\n",
    "In the cell below, I've defined a list of strings and assigned it to a variable called `greek`. Make sure to run this cell before you continue."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "greek = [\"alpha\", \"beta\", \"gamma\", \"delta\", \"epsilon\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Okay. Your job in the next cell is to write an expression that evaluates to a list of strings in `greek` that contain *exactly five letters*.\n",
    "\n",
    "Expected output: `[\"alpha\", \"gamma\", \"delta\"]`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the cell below, write an expression that evaluates to a string containing each of the *first letters* of each string in `greek`. Hint: You'll need to use a list comprehension and the `.join()` method.\n",
    "\n",
    "Expected output: `'abgde'`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Task 6: For loops\n",
    "\n",
    "The cell below has the skeleton of the `for` loop written for you. Replace the string `\"blip\"` with an expression such that the cell, when executed, outputs the first ten multiples of five.\n",
    "\n",
    "Expected output:\n",
    "\n",
    "    0\n",
    "    5\n",
    "    10\n",
    "    15\n",
    "    20\n",
    "    25\n",
    "    30\n",
    "    35\n",
    "    40\n",
    "    45\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "blip\n",
      "blip\n",
      "blip\n",
      "blip\n",
      "blip\n",
      "blip\n",
      "blip\n",
      "blip\n",
      "blip\n",
      "blip\n"
     ]
    }
   ],
   "source": [
    "for i in range(10):\n",
    "    print(\"blip\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using the variable `greek` that was defined earlier, write a `for` loop in the cell below so that when the cell is executed it outputs, on separate lines, each of the elements of the list in upper case.\n",
    "\n",
    "Expected output:\n",
    "\n",
    "    ALPHA\n",
    "    BETA\n",
    "    GAMMA\n",
    "    DELTA\n",
    "    EPSILON"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You're done! Good job."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
